package work.vcloud.baseinfo.controller;

import work.vcloud.baseinfo.utils.ExportExcelUtil;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Scope("prototype")
public class TemplateController extends BaseController {

	@Value("${web.upload_path}")
	private String filePath;
	
	@Autowired
	OpenDao openDao;
	
	/**
	 * 查询选项框列表
	 */
	public void selectList(List<Map<String, Object>> headList) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		for(int i = 0;i<headList.size();i++) {
			Map<String, Object> headMap = headList.get(i);
			if (headMap.get("fk_rule") != null) {
				map.put("fk_rule", headMap.get("fk_rule").toString());
				map.put("sqlMapId", "dictionary.selectByRule");
				List<Map<String, Object>> result = openDao.queryForList(map);
				if(result.size() > 0) {
					//String[] strArr = new String[result.size()];
//					List<String> strArr = new ArrayList<String>();
//					for(int j = 0;j < result.size(); j++) {
//						//strArr[j] = result.get(j).get("name").toString();
//						strArr.add(result.get(j).get("name").toString());
//					}
					headMap.put("list", result);
				}
			}
			if (headMap.get("fk_form") != null && !headMap.get("fk_form").equals("sc_user") && !headMap.get("fk_form").equals("sc_prop") && !headMap.get("fk_form").equals("sc_class_teacher")) {
				StringBuffer buffer = new StringBuffer();

				//排序
				if(headMap.get("fk_form").toString().equals("sc_grade")){ //年级
					buffer.append("(select name from baseinfo." + headMap.get("fk_form") + " where status = '0' order by cast(sort as INTEGER)) " + headMap.get("col"));
				}else{
					buffer.append("(select name from baseinfo." + headMap.get("fk_form") + " where status = '0' order by name) " + headMap.get("col"));
				}
				map.put("exportParams", buffer);
				map.put("sqlMapId", "port.selectImport");

				if (headMap.get("fk_form").toString().equals("sc_class")){ //班级
					map.put("sqlMapId","class.selectForExcel");
				}
				if (headMap.get("fk_form").toString().equals("sc_course")){ //课程
					map.put("sqlMapId","course.select");
					map.put("fk_unit","self");
				}

				List<Map<String, Object>> result = openDao.queryForList(map);
				if(result.size() > 0) {
//					String[] strArr = new String[result.size()];
//					for(int j = 0;j < result.size(); j++) {
//						strArr[j] = result.get(j).get("name").toString();
//					}
					headMap.put("list", result);
				}
			}
			
		}
	}
	
	/**
	 * 导出教师模板
	 */
	@RequestMapping("api/teacher.template")
	public OutputVo templateTeacher() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_user_teacher");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "教师模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	
	/**
	 * 导出学生模板
	 */
	@RequestMapping("api/student.template")
	public OutputVo templateStudent() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_user_student");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")||head.get("col").toString().equals("gzdwpk")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "学生模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	
	/**
	 * 导出主管单位模板
	 */
	@RequestMapping("api/unit.templateCenter")
	public OutputVo templateCenter() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_unit_center");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "主管单位模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	
	/**
	 * 导出学校模板
	 */
	@RequestMapping("api/unit.templateSchool")
	public OutputVo templateSchool() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_unit_school");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "学校模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	
	/**
	 * 导出附属学校模板
	 */
	@RequestMapping("api/unit.templateAttach")
	public OutputVo templateAttach() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_unit_school_attach");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "附属学校模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	
	/**
	 * 导出年级模板
	 */
	@RequestMapping("api/grade.template")
	public OutputVo templateGrade() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_unit_grade_relate");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "年级模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	/**
	 * 导出班级模板
	 */
	@RequestMapping("api/class.template")
	public OutputVo templateClass() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_class");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "班级模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	/**
	 * 导出课程模板
	 */
	@RequestMapping("api/course.template")
	public OutputVo templateCourse() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_course");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			//修复校级权限导出模板出错
			/*if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")||head.get("col").toString().equals("flag")) {
						headList.remove(i);
					}
				}
			}*/
			
			selectList(headList);
			String fileName = "课程模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	
	/**
	 * 导出上课时间模板
	 */
	@RequestMapping("api/course_time.template")
	public OutputVo templateCourseTime() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_course_time");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "上课时间模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
	
	/**
	 * 导出任课信息模板
	 */
	@RequestMapping("api/course_info.template")
	public OutputVo templateCourseInfo() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("ifimport", "1");
			map.put("form", "sc_course_info");
			map.put("sqlMapId", "port.select");
			List<Map<String, Object>> headList = openDao.queryForList(map);
			
			map.put("sqlMapId", "role.selectType");
			Integer result = Integer.valueOf(openDao.queryForList(map).get(0).get("type").toString());
			if(result>1) {
				for(int i=0;i<headList.size();i++) {
					Map<String, Object> head = headList.get(i);
					if(head.get("col").toString().equals("fk_unit")) {
						headList.remove(i);
					}
				}
			}
			
			selectList(headList);
			String fileName = "任课信息模板.xlsx";
	        ExportExcelUtil.writeExcel(headList,filePath+"excelTemplate/", fileName);
	        
	        return output("0000", "", "excelTemplate/"+fileName);	
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "", e.getMessage().toString());
		}
	}
}